package Multithreading.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * ClassName:MyThread
 * Package:Multithreading.Lock
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/3/5-20:54
 * @Version:v1.0
 */
public class MyThread extends Thread {
    static int ticket = 0;
    static ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            //同步代码块
            //   obj/MyThread.class
//            synchronized (MyThread.class) {
            lock.lock();
            //3.判断共享数据是否到了末尾,如果到了末尾
            try {
                if (ticket == 100) {
                    break;
                } else {
                    //4.判断共享数据是否到了末尾,如果没有到末尾
                    Thread.sleep(100);
                    ticket++;
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "在卖第" + ticket + "张票!!");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

//            }
        }
    }
}
